package com.smartstate.iot.tool;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartstate.iot.config.web.SpringUtils;
import com.smartstate.iot.mqtt.client.MyMQTTClient;
import com.smartstate.iot.mqtt.common.GlobalMqttParam;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

//@Component
public class ScheduleTask {
    GlobalMqttParam globalMqttParam = SpringUtils.getBean(GlobalMqttParam.class);
    String broker = globalMqttParam.getBroker();
    String userName = globalMqttParam.getUserName();
    String passWord = globalMqttParam.getPassWord();
    String clientId1 = globalMqttParam.getSubClientId1() + System.currentTimeMillis();
    int keepalive = globalMqttParam.getKeepalive();
    int timeout = globalMqttParam.getTimeout();
    MyMQTTClient myMQTTClient = new MyMQTTClient(broker, userName, passWord, clientId1, keepalive, timeout);
    JSONObject msg = new JSONObject();
    JSONObject msg1 = new JSONObject();
    JSONArray content = new JSONArray();
    JSONArray content1 = new JSONArray();
    JSONObject contentList = new JSONObject();
    JSONObject contentList1 = new JSONObject();
    JSONObject contentList2 = new JSONObject();
    JSONObject contentList3 = new JSONObject();
    JSONObject contentList4 = new JSONObject();
    JSONObject contentList5 = new JSONObject();
    JSONObject contentList6 = new JSONObject();
    Random random = new Random();
    int activeEnergy = 10;
    int reactiveEnergy = 15;
    int apparentEnergy = 20;

//    @Scheduled(cron = "0/1 * * * * ? ")
    public void send() {
        long time = new Date().getTime();
        msg1.put("deviceName", "DHU0005");
        msg1.put("deviceType", "DHU0005");
        msg1.put("timesTamp", String.valueOf(time));
        msg1.put("TS", "yyyy-MM-dd HH:mm:ss");
        msg1.put("deviceId", "WG003");
        msg1.put("version", "1.0.0");
        msg1.put("configured","true");
        int num = 10 + (random.nextInt(10));
        int num1 = 15 + (random.nextInt(10));
        int num2 = 20 + (random.nextInt(10));
        activeEnergy += random.nextInt(10);
        reactiveEnergy += random.nextInt(10);
        apparentEnergy += random.nextInt(10);
        contentList.put("DHU0007.activeEnergy",String.valueOf(activeEnergy));
        contentList.put("DHU0007.reactiveEnergy",String.valueOf(reactiveEnergy));
        contentList.put("DHU0007.apparentEnergy",String.valueOf(apparentEnergy));
        content1.add(contentList);
        msg1.put("content",content1);
        //  DHU0005
        msg.put("deviceName", "DHU0005");
        msg.put("deviceType", "DHU0005");
        msg.put("timesTamp", String.valueOf(time));
        msg.put("TS", "yyyy-MM-dd HH:mm:ss");
        msg.put("deviceId", "WG004");
        msg.put("version", "1.0.0");
        msg.put("configured","true");
        contentList1.put("DHU0005.longParticle", String.valueOf(num2));
        contentList1.put("DHU0005.littleParticle", String.valueOf(num1));
        contentList1.put("DHU0005.dewPoint", String.valueOf(num));
        contentList1.put("DHU0005.temperature", String.valueOf(num2));
        contentList1.put("DHU0005.humidity", String.valueOf(num));
        contentList1.put("DHU0005.staticPressure", String.valueOf(num1));
        content.add(contentList1);
        //  DHU0001
        contentList2.put("DHU0001.longParticle", String.valueOf(num2));
        contentList2.put("DHU0001.littleParticle", String.valueOf(num1));
        contentList2.put("DHU0001.dewPoint", String.valueOf(num));
        contentList2.put("DHU0001.temperature", String.valueOf(num2));
        contentList2.put("DHU0001.humidity", String.valueOf(num));
        contentList2.put("DHU0001.staticPressure", String.valueOf(num1));
        content.add(contentList2);
        //      DHU0002
        contentList3.put("DHU0002.longParticle", String.valueOf(num2));
        contentList3.put("DHU0002.littleParticle", String.valueOf(num1));
        contentList3.put("DHU0002.dewPoint", String.valueOf(num));
        contentList3.put("DHU0002.temperature", String.valueOf(num2));
        contentList3.put("DHU0002.humidity", String.valueOf(num));
        contentList3.put("DHU0002.staticPressure", String.valueOf(num1));
        content.add(contentList3);
        //  DHU0003
        contentList4.put("DHU0003.longParticle", String.valueOf(num2));
        contentList4.put("DHU0003.littleParticle", String.valueOf(num1));
        contentList4.put("DHU0003.dewPoint", String.valueOf(num));
        contentList4.put("DHU0003.temperature", String.valueOf(num2));
        contentList4.put("DHU0003.humidity", String.valueOf(num));
        contentList4.put("DHU0003.staticPressure", String.valueOf(num1));
        content.add(contentList4);
        //  DHU0004
        contentList5.put("DHU0004.longParticle", String.valueOf(num2));
        contentList5.put("DHU0004.littleParticle", String.valueOf(num1));
        contentList5.put("DHU0004.dewPoint", String.valueOf(num));
        contentList5.put("DHU0004.temperature", String.valueOf(num2));
        contentList5.put("DHU0004.humidity", String.valueOf(num));
        contentList5.put("DHU0004.staticPressure", String.valueOf(num1));
        content.add(contentList5);
        //  DHU0006
        contentList6.put("DHU0006.longParticle", String.valueOf(num2));
        contentList6.put("DHU0006.littleParticle", String.valueOf(num1));
        contentList6.put("DHU0006.dewPoint", String.valueOf(num));
        contentList6.put("DHU0006.temperature", String.valueOf(num2));
        contentList6.put("DHU0006.humidity", String.valueOf(num));
        contentList6.put("DHU0006.staticPressure", String.valueOf(num1));
        content.add(contentList6);
        msg.put("content", content);
        try {
            myMQTTClient.connect();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
        myMQTTClient.publish(JSON.toJSONString(msg), "SH/HK/NJ/UP/0001");
        myMQTTClient.publish(JSON.toJSONString(msg1), "SH/HK/NJ/UP/0001");
        content.clear();
        content1.clear();
    }



//    @Scheduled(cron = "0/5 * * * * ? ")
    public void heart(){
        HashMap<String, Object> hashMap = new HashMap<>();
        long time = new Date().getTime();
        hashMap.put("timesTamp",String.valueOf(time));
        hashMap.put("config","true");
        ArrayList<Map> maps = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("ip","192.168.35.12");
        map.put("port","1926");
        map.put("deviceType","工业看板");
        map.put("deviceName","工业看板003");
        map.put("newTopic","SH/HK/NJ/UP/WG003");
        map.put("deviceId","WG003");
        map.put("cpu",random.nextInt(50)+30);
        map.put("momery",random.nextInt(50)+40);
        maps.add(map);
        hashMap.put("content",maps);
        try {
            myMQTTClient.connect();
            myMQTTClient.publish(JSON.toJSONString(hashMap),"SH/HK/NJ/HEART");
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

//    @Scheduled(cron = "0/5 * * * * ? ")
    public void heart1(){
        HashMap<String, Object> hashMap = new HashMap<>();
        long time = new Date().getTime();
        hashMap.put("timesTamp",String.valueOf(time));
        hashMap.put("config","true");
        ArrayList<Map> maps = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("ip","192.168.59.12");
        map.put("port","8563");
        map.put("deviceType","工业看板");
        map.put("deviceName","工业看板004");
        map.put("newTopic","SH/HK/NJ/UP/WG004");
        map.put("deviceId","WG004");
        map.put("cpu",random.nextInt(50)+20);
        map.put("momery",random.nextInt(50)+30);
        maps.add(map);
        hashMap.put("content",maps);
        try {
            myMQTTClient.connect();
            myMQTTClient.publish(JSON.toJSONString(hashMap),"SH/HK/NJ/HEART");
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}
